# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2021/12/9 11:52
# @Author  : zuodengbo
# @Email   : zuodb@amoydx.com
# @File    : send_sms
# @Software: PyCharm
import json
from http import client
import urllib
from config import SmsConfig

sms = SmsConfig()


def send_sms(text, mobile):
    params = urllib.parse.urlencode(
        {'account': sms.account, 'password': sms.password, 'content': text, 'mobile': mobile, 'format': 'json'})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = client.HTTPConnection(sms.host, port=80, timeout=30)
    conn.request("POST", sms.sms_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read().decode()
    conn.close()
    resp_json = json.loads(response_str)
    return resp_json["code"], resp_json["msg"], resp_json["smsid"]


if __name__ == '__main__':
    send_sms("牛逼", "12345678900")